package net.sppan.blog.controller.admin;

import java.util.Date;
import java.util.List;

import net.sppan.blog.annotation.RequiresPermissions;
import net.sppan.blog.controller.BaseController;
import net.sppan.blog.model.Deduction;
import net.sppan.blog.model.User;
import net.sppan.blog.utils.Const;

import org.apache.commons.lang.StringUtils;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;

@RequiresPermissions(key = { "admin:sys:deduct" })
public class AdminDeductController extends BaseController {
	@RequiresPermissions(key = { "admin:sys:deduct:list" })
	public void list() {
		String searchOfferId = getPara("search_offerid");
		String searchAgentId = getPara("search_agentid");
		String searchDate = getPara("search_date");
		int pageNumber = getParaToInt("pageNumber", 1);
		int pageSize = getParaToInt("pageSize", 10);
		String sql = " from sys_deduction where 1=1 ";
		if (StringUtils.isNotBlank(searchOfferId)) {
			setAttr("search_offerid", searchOfferId);
			sql += " and offer_id=" + searchOfferId;
		}
		if (StringUtils.isNotBlank(searchAgentId) && !searchAgentId.equals("0")) {
			setAttr("search_agentid", searchAgentId);
			sql += " and user_id=" + searchAgentId;
		}
		if (!StrKit.isBlank(searchDate)) {
			setAttr("search_date", searchDate);
			String[] ds = searchDate.split(" - ");
			String beginDate = ds[0] + " 00:00:00";
			String endDate = ds[1] + " 23:59:59";
			sql += " and create_time between '" + beginDate + "' and '" + endDate + "'";
		}
		Page<Deduction> page = Deduction.dao.paginate(pageNumber, pageSize, "select * ", sql);
		sql += " order by id desc";
		setAttr("pageList", page);
		List<User> agents = User.dao.findUserByRoleId(Const.AgentId);
		setAttr("users", agents);
		render("list.html");
	}

	@RequiresPermissions(key = { "admin:sys:deduct:list" })
	public void add_edit() {
		Integer id = getParaToInt("id");
		if (id != null) {
			Deduction d = Deduction.dao.findById(id);
			setAttr("deduct", d);
		}
		List<User> agents = User.dao.findUserByRoleId(Const.AgentId);
		setAttr("users", agents);
		render("form.html");
	}

	/**
	 * 新增-更新
	 */
	@RequiresPermissions(key = { "admin:sys:deduct:list" })
	public void save_update() {
		Deduction deduct = getModel(Deduction.class);
		if (deduct.getUserId() == null || deduct.getUserId() == 0) {
			deduct.setUserId(null);
		}
		Integer agentId = deduct.getUserId() == null ? 0 : deduct.getUserId();
		Integer offerId = deduct.getOfferId() == null ? 0 : deduct.getOfferId();
		Deduction d1 = Deduction.dao.findFirst("select * from sys_deduction where user_id=" + agentId);
		Deduction d2 = Deduction.dao.findFirst("select * from sys_deduction where offer_id=" + offerId);
		boolean isSuccess = false;
		if (deduct.getId() == null) {
			if (d1 != null || d2 != null) {
				renderJson(isSuccess, "add failed，already exists");
				return;
			}
			deduct.setCreateTime(new Date());
			isSuccess = deduct.save();
			if (isSuccess) {
				renderJson(isSuccess, "add success");
			} else {
				renderJson(isSuccess, "add failed");
			}
		} else {
			deduct.setUpdateTime(new Date());
			isSuccess = deduct.update();
			renderJson(isSuccess, "edit success");
		}
	}

	/**
	 * 删除
	 */
	@RequiresPermissions(key = { "admin:sys:deduct:list" })
	public void delete() {
		Integer id = getParaToInt("id");
		boolean isSuccess = false;
		isSuccess = Deduction.dao.deleteById(id);
		if (isSuccess) {
			renderJson(isSuccess, "delete success");
		} else {
			renderJson(isSuccess, "delete failed");
		}
	}
}
